package dao

import (
	"gitee.com/mangenotwork/game-douniu/api/common"
	"gitee.com/mangenotwork/game-douniu/api/entity"
	"github.com/mangenotwork/common/log"
	"github.com/mangenotwork/common/utils"
)

type UserDao struct {
}

// 是否重复
func (dao *UserDao) IsRepeat(account, nickName string) bool {
	db := common.GetGorm("test")
	data := &entity.UserBase{}
	err := db.Model(&entity.UserBase{}).Where("account=? OR nickName=?", account, nickName).Find(&data).Error
	if err != nil {
		log.Error(err)
	}
	if data.UserId > 0 {
		return false
	}
	return true
}

func (dao *UserDao) Register(userBase *entity.UserBase) (*entity.UserBase, error) {
	db := common.GetGorm("test")
	err := db.Model(&entity.UserBase{}).Create(userBase).Error
	log.Info("userBase", userBase)
	return userBase, err
}

func (dao *UserDao) Login(account, password string) (*entity.UserBase, error) {
	db := common.GetGorm("test")
	data := &entity.UserBase{}
	err := db.Model(&entity.UserBase{}).Where("account=? And password=?", account, password).Find(&data).Error
	return data, err
}

func (dao *UserDao) GetUser(userId int64) (*entity.UserBase, error) {
	db := common.GetGorm("test")
	data := &entity.UserBase{}
	err := db.Model(&entity.UserBase{}).Where("user_id=?", userId).Find(&data).Error
	return data, err
}

func (dao *UserDao) Modify(userId int64, nickName, avatar string) error {
	db := common.GetGorm("test")
	ex := map[string]any{"nickName": nickName, "avatar": avatar}
	return db.Model(&entity.UserBase{}).Where("user_id=?", userId).Updates(ex).Error
}

func (dao *UserDao) GetUserList(userIds []int64) ([]*entity.UserBase, error) {
	db := common.GetGorm("test")
	data := make([]*entity.UserBase, 0)
	err := db.Model(&entity.UserBase{}).Where("user_id in (?)", userIds).Find(&data).Error
	return data, err
}

func (dao *UserDao) GetUserMap(userIds []int64) (map[int64]*entity.UserBase, error) {
	dataMap := make(map[int64]*entity.UserBase)
	list, err := dao.GetUserList(userIds)
	if err != nil {
		return dataMap, err
	}
	for _, v := range list {
		dataMap[v.UserId] = v
	}
	return dataMap, err
}

func (dao *UserDao) SetVIP(userId int64) error {
	db := common.GetGorm("test")
	ex := map[string]any{"is_vip": 1}
	return db.Model(&entity.UserBase{}).Where("user_id=?", userId).Updates(ex).Error
}

func (dao *UserDao) DeleteVIP(userId int64) error {
	db := common.GetGorm("test")
	ex := map[string]any{"is_vip": 0}
	return db.Model(&entity.UserBase{}).Where("user_id=?", userId).Updates(ex).Error
}

// 分页查询用户列表
func (dao *UserDao) GetUserListByPg(page, limit, userId int64, nickName string) (int64, []*entity.UserBase, error) {
	db := common.GetGorm("test")

	result := make([]*entity.UserBase, 0)
	db = db.Model(&entity.UserBase{})

	if userId > 0 {
		db = db.Where("user_id = ?", userId)
	}

	if len(nickName) > 0 {
		db = db.Where("nickName = ?", nickName)
	}

	var count int64 = 0
	err := db.Count(&count).Error
	if err != nil {
		return count, result, err
	}

	err = db.Order("user_id desc").Scopes(Pagination(int(page), int(limit))).Find(&result).Error
	return count, result, err
}

func (dao *UserDao) UserCount() int64 {
	var count int64 = 0
	db := common.GetGorm("test")
	err := db.Model(&entity.UserBase{}).Count(&count).Error
	if err != nil {
		log.Error(err)
	}
	return count
}

func (dao *UserDao) NewUserToDayTotal() int64 {
	var count int64 = 0
	db := common.GetGorm("test")
	err := db.Model(&entity.UserBase{}).Where("created_at >= ?", utils.BeginDayUnix()).Count(&count).Error
	if err != nil {
		log.Error(err)
	}
	return count
}
